<template>
  <div class="shopcar">
    <div class="goodsList">
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner" v-for="(item, i) in moviesList" :key="item.id">
            <mt-switch
              @change="selectChanged(item.id, $store.getters.getSelected[item.id])"
              v-model="$store.getters.getSelected[item.id]"
            ></mt-switch>
            <img :src="item.img" :alt="item.titleCn">
            <div class="info">
              <h2>{{item.titleCn}}</h2>
              <p>
                <span class="price">￥29.9</span>
                <numbox :goodsId="item.id" :initCount="$store.getters.getGoodsCount[item.id]"></numbox>
                <a href="#" @click.prevent="remove(item.id, i)">删除</a>
                {{ $store.getters.getGoodsCount[item.id]}}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner willpay">
          <div class="check">
            <p>总计：（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.getCountAndAmount.count }}</span>件，总价
              <span class="red">{{ $store.getters.getCountAndAmount.amount }}</span>元
            </p>
          </div>
          <mt-button type="danger">结算</mt-button>
        </div>
      </div>
    </div>
    {{ $store.getters.getSelected }}
    <p></p>
  </div>
</template>

<script>
import numbox from '../subcomponent/shopcar_numbox.vue'
export default {
  data() {
    return {
      moviesList: []
    }
  },
  created() {
    this.getgoods()
  },
  methods: {
    getgoods() {
      var idArr = []
      this.$store.state.car.forEach(item => idArr.push(item.id))
      if (idArr.length <= 0) return
      this.$http.get('../../data_file/movies.json')
        .then(data => {
          data = data.body.movies
          // console.log(data);

          for (let index = 0; index < idArr.length; index++) {
            const element = idArr[index];
            console.log(element);
            data[element].id = element
            this.moviesList.push(data[element])
          }
        })
    },
    remove(id, index) {
      // 点击删除 把数据从 store 中 和当前组件中的 goodlist 列表，使用相对应的index 删除
      this.$store.commit('removeFromCar', id)
      this.moviesList.splice(index, 1)
    },
    selectChanged(id, val) {
      // 没当点击开关把最新的状态同步到 store 中
      console.log(id + '---' + val);
      this.$store.commit('updateSelect', { id, selected: val })
    }
  },
  components: {
    numbox
  }
}
</script>

<style lang="scss" scoped>
.shopcar {
  background-color: #eee;
  overflow: hidden;
  .goodsList {
    img {
      width: 50px;
      height: 80px;
    }
    h2 {
      font-size: 13px;
    }
    .mui-card-content-inner {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .willpay {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: bold;
      font-size: 1;
    }
  }
}
</style>
